// pages/user/goEvaluate/goEvaluate.js
var app = getApp();
var getImgarr=[];//需要过度展示图片
var upimgarr=[];//上传七牛云成功后返给后台的图片
var uping = false;
var valcontent= '';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    starnum: 0,
    stars: [0, 1, 2, 3, 4],
    srcXinA: '../../../images/icon_sxin.png',
    srcXinB: '../../../images/icon_xin.png',
    noteMaxLen: 100,
    currentNoteLen: 0,
    imgUrl: [],
    upchoosebox:[],
    shooseList: [], //标签数组
    order_id: '', //订单id
    orderMsgShow: '', //订单信息
    getLabelIdbox:[],//提交标签id
    switchs:false,//false 为关闭 true 为生效
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    getImgarr = [];//显示图片置空
    upimgarr = [];//上传的图片置空
    that.setData({
      order_id: options.order_id
    })
    wx.showLoading({
      title: '加载中...',
    })
    that.getOrderDetail();
    that.getEvaluateType();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  //输入内容字数限制
  charChange: function(e) {
    var value = e.detail.value;
    var len = parseInt(value.length);
    if (len > this.data.noteMaxLen) return;

    this.setData({
      currentNoteLen: len, //当前字数 
    });
    valcontent = e.detail.value
  },

  //评价星
  shooseStr: function(e) {
    let starnum = parseInt(e.target.dataset.starnum) || 0;
    this.setData({
      starnum: starnum,
    });
  },
  // 上传图片
  chooseImg: function() {
    var that = this;
    var num = 3 - getImgarr.length;
    if (uping) {
      return false
    }
    wx.chooseImage({
      count: num, // 默认9  
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有  
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片 
        var tempFilePaths = res.tempFilePaths
        var url = that.data.imgUrl.concat(tempFilePaths);
        for (var k = 0; k < url.length;k++){
          that.uplodImg(url[k]);//循环上传图片
        }
      }
    })
  },
  //上传图片到服务器
  uplodImg(chengimg){
    var that=this;
    wx.showLoading({
      title: '上传图片中',
    })
    uping=true;
    wx.uploadFile({
      url: app.globalData._url + 'Common/Common/upload',
      filePath: chengimg,
      name: 'file',
      success: function (res) {
        console.log('res',res);
        wx.hideLoading();
        var data = JSON.parse(res.data);   
        console.log('data',data);
        if (data.code == 1000) {
          getImgarr.push(data.data.all_url);
          upimgarr.push(data.data.url);
          that.setData({
            imgUrl: getImgarr,
            upchoosebox: upimgarr
          })
        } else {
          app.showToast("上传头像失败");
        }
      },
      complete: function () {
        uping = false
      }
    })
},
  //删除图片
  detailImg: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.detail;
    getImgarr.splice(index, 1);
    upimgarr.splice(index, 1);
    if (that.data.imgUrl.length == 0) {
      that.setData({
        imgUrl: getImgarr,
        upchoosebox: upimgarr
      })
    } else {
      that.setData({
        imgUrl: getImgarr,
        upchoosebox: upimgarr
      })
    }
  },
  //点击选中的效果 
  chooseit(e) {
    var that = this, indexs = e.target.dataset.indexs, label_id = e.target.dataset.labelid;
    var getlist = that.data.shooseList;
    var getLabelList = that.data.getLabelIdbox;
    if (getlist[indexs].onit == false) {
      getlist[indexs].onit = true;
      getLabelList.push(label_id);
    } else {
      getlist[indexs].onit = false;
      var onwhere = getLabelList.indexOf(label_id);
      getLabelList.splice(onwhere,1);
    }
    that.setData({
      shooseList: getlist,
      getLabelIdbox: getLabelList
    })
  },
  //获取订单详情
  getOrderDetail() {
    var that = this;
    var order_id = that.data.order_id;
    app.ajax({
      url: 'User/Order/getOrderDetail',
      method: "POST",
      data: {
        order_id: order_id
      },
      success: function(res) {
        wx.hideLoading();
        if (res.data.code = 1000) {
          that.setData({
            orderMsgShow: res.data.data
          })
        } else {
          app.showToast(res.data.message, "none", 2000, function() {});
        }
      }

    })
  },
  //获取订单详情
  getEvaluateType() {
    var that = this;
    var order_id = that.data.order_id;
    app.ajax({
      url: 'User/Order/getEvaluateLabel',
      method: "POST",
      data: {
        order_id: order_id
      },
      success: function(res) {
        wx.hideLoading();
        if (res.data.code = 1000) {
          var gettablelist = res.data.data;
          for (var i = 0; i < gettablelist.length; i++) {
            gettablelist[i].onit = false;
          }
          that.setData({
            shooseList: gettablelist
          })
        } else {
         // app.showToast(res.data.message, "none", 2000, function() {});
        }
      }
    })
  },
  //提交评论
  upEvaluate() {
    var that = this;
    var order_id = that.data.orderMsgShow.order_id;
    var equipments_id = that.data.orderMsgShow.equipments_id;
    var evaluate_stars = that.data.starnum;
    var getLabox = that.data.getLabelIdbox;
    var evaluate_image = upimgarr;
    if (order_id==''){
      app.showToast('订单号为空,请重进商品评价', "none", 2000, function () { });
      return false;
    }
    if (evaluate_stars <=0 ) {
      app.showToast('亲,评个星级平分吧', "none", 2000, function () { });
      return false;
    }
    if (getLabox.length <=0) {
      app.showToast('亲,先选择一个标签吧', "none", 2000, function () { });
      return false;
    }
    if (valcontent == '') {
      app.showToast('评价内容不能为空', "none", 2000, function () { });
      return false;
    }
    //设置一个开关
    if (that.data.switchs==true){
      return false;
    }
    that.setData({
      switchs: true
    })
    wx.showLoading({
      title: '提交评论中',
    })
    app.ajax({
      url: 'User/Order/addUserEvaluation',
      method: "POST",
      data: {
        type:1,
        order_id: order_id,
        equipments_id: equipments_id,
        evaluate_content: valcontent,
        evaluate_stars: evaluate_stars,
        label: getLabox,
        evaluate_image: evaluate_image
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.code = 1000) {
          app.showToast(res.data.message, "none", 2000, function () { });
          setTimeout(() => {
            wx.navigateBack({
             delta: 1
            })
          }, 1000);
        } else {
          app.showToast(res.data.message, "none", 2000, function () { });
        }
        setTimeout(() => {
          that.setData({
            switchs: false
          })
        }, 3000);
      }

    })
  },
})